/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.support.ui.frame;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import vn.support.system.SystemProperties;
import vn.support.system.SystemShutdown;

/**
 *
 * @author nttuyen
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int init = SystemProperties.init();
        if(init == SystemProperties.SYSTEM_START_ALREADY) {
            System.out.println("Already!");
            return;
        } else if(init == SystemProperties.SYSTEM_START_FAILURE) {
            JOptionPane.showMessageDialog(null, SystemProperties.getValue("support.chat.system.start.failure"));
            return;
        }
        try {
            //LookAndFeel
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
            
            //ShutdownHook
            Runtime.getRuntime().addShutdownHook(new SystemShutdown());

            //Tray icon
            if(SystemTray.isSupported()) {
                SystemTray tray = SystemTray.getSystemTray();
                TrayIcon trayIcon = new TrayIcon(new ImageIcon(Main.class.getResource(SystemProperties.getValue("support.chat.system.tray.icon"))).getImage());
                trayIcon.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JFrame frame = SystemProperties.get(SystemProperties.MAIN_FRAME);
                        if(frame != null) {
                            frame.setVisible(true);
                            if(frame.getState() != JFrame.NORMAL) {
                                frame.setState(JFrame.NORMAL);
                            }
                            frame.setAlwaysOnTop(true);
                            frame.toFront();
                            frame.requestFocus();
                            frame.setAlwaysOnTop(false);
                            frame.repaint();
                        }
                    }
                });
                trayIcon.setToolTip(SystemProperties.getValue("support.chat.system.tray.menu.tooltip"));
                SystemProperties.set(SystemProperties.TRAY_ICON, trayIcon);

                //Default popup
                PopupMenu popup = new PopupMenu();
                MenuItem exitMenu = new MenuItem(SystemProperties.getValue("support.chat.system.tray.menu.exit"));
                exitMenu.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ((JFrame)SystemProperties.get(SystemProperties.MAIN_FRAME)).dispose();
                        System.exit(0);
                    }
                });
                popup.add(exitMenu);
                trayIcon.setPopupMenu(popup);
                try {
                    tray.add(trayIcon);
                } catch (AWTException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            //Run
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    SystemProperties.setLocale(Locale.ROOT);
                    LoginViewFrame login = new LoginViewFrame();
                    SystemProperties.set(SystemProperties.MAIN_FRAME, login);
                    login.setVisible(true);
                }
            });
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
